﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2022 kingerrobot
// 版权所有。 
//
// 文件名：BRcsEquipCuring
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2022/1/19 13:52:39
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
using K8.MDCBB;
using KingerRobot.MexicoSailunWMS.Spi;

namespace KingerRobot.MexicoSailunWMS.SpiService;
internal class BRcsEquipCuring : IRcsEquipCuring
{

    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    public PORcsEquipCuring GetCuringById(long id)
    {
        var db = Db.Default;

        // 查找硫化机
        var result = db.RcsEquipCurings.Where(t => t.Id == id).FirstOrDefault();

        return result;
    }
    public CuringLRDTO GetCuringRLByCuringLocId(long curingLocId)
    {
        var curingId = _WcsEquipLoc.GetEquipIdByLocId(curingLocId);

        // 设备是否停止【停止(已有请料输送至异常口)】
        return GetCuringRLByCuringId(curingId);
    }

    public CuringLRDTO GetCuringRLByCuringId(long curingId)
    {
        var curing = GetCuringByCuringId(curingId);

        // 设备是否停止【停止(已有请料输送至异常口)】
        return GetCuringRLReault(curing);
    }

    public List<PORcsEquipCuring> GetEquipCurings()
    {
        var db = Db.Default;

        // 查找硫化机
        var result = db.RcsEquipCurings
            .Join(db.RcsEquips, f => f.Id, s => s.Id, (f, s) => new { Curing = f, Equip = s })
            .Where(t => t.Equip.EquipEnable == 1 && t.Equip.ErrCode == 0)
            .Select(t => t.Curing)
            .ToList();

        return result;
    }

    public List<PORcsEquipCuring> GetAllEquipCurings()
    {
        var db = Db.Default;

        // 查找硫化机
        var result = db.RcsEquipCurings
            .Join(db.RcsEquips, f => f.Id, s => s.Id, (f, s) => new { Curing = f, Equip = s })
            .Where(t => t.Curing.UseFlag == 1)
            .Select(t => t.Curing)
            .ToList();
        return result;
    }

    public List<CuringEquipAndLocBean> GetAllEquipCuringLoc()
    {
        var db = Db.Default;

        // 查找硫化机
        var result = db.RcsEquipCurings
            .Join(db.WcsEquipLocs, f => f.Id, s => s.EquipId, (f, s) => new { Curing = f, EquipLoc = s })
            .Select(t => new CuringEquipAndLocBean
            {
                PORcsEquipCuring = t.Curing,
                POWcsEquipLoc = t.EquipLoc,
            }).ToList();

        return result;
    }

    public List<PORcsEquipCuring> GetEquipCuringsByCode(string curingCode)
    {
        // 查找硫化机
        var result = GetEquipCurings()
            .Where(t => t.CuringCode == curingCode)
            .ToList();
        return result;
    }

    public PORcsEquipCuring GetCuringByCuringId(long curingId)
    {
        var db = Db.Default;

        // 查找硫化机
        var result = db.RcsEquipCurings
            .Join(db.RcsEquips, f => f.Id, s => s.Id, (f, s) => new { Curing = f, Equip = s })
            .Where(t => t.Equip.EquipEnable == 1 && t.Equip.ErrCode == 0 && t.Curing.Id == curingId)
            .Select(t => t.Curing)
            .FirstOrDefault();

        return result;
    }


    public long? GetOtherHalfCuring(long targetLoc)
    {

        var curingId = _WcsEquipLoc.GetEquipIdByLocId(targetLoc);

        var curing = GetCuringByCuringId(curingId);
        if (curing == null)
        {
            return null;
        }

        var result = GetEquipCuringsByCode(curing.CuringCode).Where(t => t.Id != curingId).FirstOrDefault();
        if (result == null)
        {
            return 0;
        }

        var locId = _WcsEquipLoc.GetLocIdByEquipId(result.Id.GetValueOrDefault());
        return locId;
    }

    public CuringLRDTO GetCuringRL(long curingId)
    {
        var curing = GetCuringByCuringId(curingId);

        // 设备是否停止【停止(已有请料输送至异常口)】
        return GetCuringRLReault(curing);
    }

    public CuringLRDTO GetCuringRLReault(PORcsEquipCuring curing)
    {
        // 设备是否停止【停止(已有请料输送至异常口)】
        if (curing is null || curing.CuringStatus == 2)
        {
            return null;
        }
        string mode;
        // 1:(1号右模；2号左模)；2:(1号左模；2号右模)
        if (curing.CuringSign == 1)
        {
            mode = curing.CuringModeIndex == 1 ? "R" : "L";
        }
        else
        {
            mode = curing.CuringModeIndex == 1 ? "L" : "R";
        }
        return new()
        {
            CuringId = curing.Id.GetValueOrDefault(),
            CuringCode = curing.CuringCode,
            Mode = mode,
            ModeIndex = curing.CuringModeIndex.GetValueOrDefault(),
        };
    }

    public bool IsPairCuring(long targetLoc, long otherTargetLoc)
    {
        var db = Db.Default;
        var equips = db.WcsEquipLocs.Where(t => t.LocId == targetLoc || t.LocId == otherTargetLoc).Select(t => t.EquipId).ToList();
        if (equips.Count() != 2)
        {
            return false;
        }
        var targetEqu = equips[0];
        var otherTargetEqu = equips[1];
        var result = db.RcsEquipCurings
            .Where(t => t.Id == targetEqu || t.Id == otherTargetEqu)
            .Select(t => t.CuringCode).Distinct().Count();

        return result == 1;
    }

    public int UpdateCruingSuspend(long id)
    {
        var db = Db.Default;


        var result = 0;
        var curing = db.RcsEquipCurings
            .Where(t => t.DitchId == id).FirstOrDefault();
        if (curing is not null)
        {
            curing.CuringStatus = curing.CuringStatus == 1 ? 0 : 1;
            curing.UpdateTime = DateTime.Now;
            result = db.SaveChanges();
        }
        return result;
      
    }

    public int UpdateCuringStop(long id)
    {
        var db = Db.Default;

        var result = 0;
        var curing = db.RcsEquipCurings
            .Where(t => t.DitchId == id).FirstOrDefault();
        if (curing is not null)
        {
            curing.CuringStatus = curing.CuringStatus == 2 ? 0 : 2;
            curing.UpdateTime = DateTime.Now;
            result = db.SaveChanges();
        }
        return result;
    }
    public List<string> GetCuringCodesByDitchId(long ditchId)
    {
        var db = Db.Default;
        var result = db.RcsEquipCurings.Where(t => t.DitchId == ditchId && t.UseFlag == 1)
            .GroupBy(t => t.CuringCode).Select(t => new
            {
                CuringCode = t.Key.ToString(),
            }).Select(t => t.CuringCode).ToList();
        return result;
    }
    public List<PORcsEquipCuring> GetCuringByCode(string curingCode)
    {
        var db = Db.Default;
        var result = db.RcsEquipCurings.Where(t => t.CuringCode == curingCode).ToList();
        return result;
    }
    public List<string> GetCuringCodes()
    {
        var db = Db.Default;
        var result = db.RcsEquipCurings.Where(t=>t.UseFlag==1).GroupBy(t => t.CuringCode).Select(t => t.Key).ToList();
        return result;
    }
}